<template>
  <!--  面包屑-->
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/main' }">主页</el-breadcrumb-item>
    <el-breadcrumb-item>
      <a href="/post">贴吧列表</a>
    </el-breadcrumb-item>
  </el-breadcrumb>

<!--  <div class="app-course">-->

      <div class="nav">
        <div class="nav-left">
          <div class="time">
            <span>时间</span>
            <div class="tubiao">
              <el-icon @click="getPostListByOrder('create_time','asc')"><CaretTop /></el-icon>
              <el-icon @click="getPostListByOrder('create_time','desc')"><CaretBottom /></el-icon>
            </div>

          </div>
          <div class="hot">
            <span>热度</span>
            <div class="tubiao">

              <el-icon @click="getPostListByOrder('like_number','asc')"><CaretTop /></el-icon>
              <el-icon @click="getPostListByOrder('like_number','desc')"><CaretBottom /></el-icon>
            </div>
          </div>
        </div>

        <el-form-item label="">
          <el-input v-model="keyword" placeholder="请输入贴吧id或名称" style="width: auto"/>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
      <el-table :data="PostList.data" border stripe style="width: 100%;">
          <el-table-column prop="title" label="标题" />
          <el-table-column prop="createTime" label="创建时间"  />
          <!-- <el-table-column prop="content" label="正文"  class="content-cell"/> -->
          <el-table-column prop="content" label="正文">
            <template v-slot="{ row }">
              <div class="content-cell">
                <span>{{ row.content }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="userName" label="发送者"/>
          <el-table-column prop="likes" label="点赞数" />
          <el-table-column prop="replies" label="回复数"  />
          <el-table-column prop="status" label="状态" />
          <el-table-column fixed="right" label="操作" width="120">
            <template #default="{row}">
              <el-button  type="primary" size="small" @click="details(row.id)">详情</el-button>
              <el-button  type="primary" size="small" @click="deletePost(row.id)">删除</el-button>
            </template>
          </el-table-column>
      </el-table>
      <div class="example-pagination-block">
        <el-pagination
            :current-page="pageNum"
            :page-size="pageSize"
            :background="true"
            layout="total, prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
            style="margin-top: 15px"
        />
      </div>
<!--    </div>-->

</template>


<script setup>
import { useRouter } from 'vue-router';
import userApi  from '../api/public/home'
import {ref, onMounted, watch, computed, onUpdated, reactive} from 'vue'
import router from "../router";


let PostList=reactive({
  id: "",
  title: "",
  content: "",
  createTime: "",
  userId: "",
  likes:"",
  replies:"",
  status: "",
});
let pageNum = ref(1)
let pageSize = ref(5)
let total = ref(0)
let orderBy = ref('likes')
let order = ref('asc')
let keyword = ref('')

const details = (id) => {
  router.push({path:'/postdetails', query: {id} });
}
const deletePost = (id) => {
  userApi.deletePost(id)
  location.reload()
}

function getPostList(val)  {
  console.log(pageNum.value)
  pageNum=val;
  console.log("page,orderBy,order=",pageNum,orderBy.value,order.value)
  userApi.findPostAPI(pageNum, orderBy.value, order.value).then((res)=>{
      console.log("res==",res)
      PostList.data = res.data.data.list ;
      console.log("PostList.data=",PostList.data);
      total.value = res.data.data.total;
      pageSize.value=res.data.data.pageSize;
      pageNum=res.data.data.pageNum;
    })
}

const getPostListByOrder = (order_by,orderr) => {
  orderBy.value = order_by
  order.value = orderr
  getPostList(pageNum.value)
}

onMounted(() => {
    orderBy.value='likes'
    order.value='desc'
  getPostList(pageNum.value);
});

const handleCurrentChange = (val) => {
          getPostList(val)
        };
onMounted(()=>{
    const el = document.querySelectorAll('.nav-left .el-icon')
    console.log("el==",el)
    el.forEach(item=>{
        item.addEventListener('click',()=>{
             const isActive = item.classList.contains('active')
             el.forEach(icon => icon.classList.remove('active'))
            if (!isActive) {
            item.classList.add('active') // 添加所点击图标的激活状态
      }

        })
    })
})

function onSearch() {
  let key = keyword.value.trim();
  console.log("params =", key);
  if(key==""){
    handleCurrentChange(pageNum.value);
  }else{
    userApi.postSearch(key).then(response => {
      console.log("response =", response);
      PostList.data= response.data.data.list;
      console.log("PostList.data =", PostList);
      pageNum=response.data.data.pageNum;
      pageSize.value=response.data.data.pageSize;
      total.value=response.data.data.total
    })
  }
}
</script>




<style scoped>
.container{
    width: 1100px;
    margin: 0 auto;


}
.app-course{
    background-color: rgb(244,245,247);
}
.time, .hot{
    display: inline-block;
    display: flex;
}
.nav{
    display: flex;
    justify-content: space-between;
    padding: 20px ;
}
.nav-left{
    display: flex;
}
.time{
    margin-right: 10px;
}
.tubiao{
    display: flex;
    flex-direction: column;
    justify-content: flex-start ;

}
.tubiao .el-icon:nth-child(2){
    position: relative;
    top:-6px;
}
.nav input{
    width: 120px;
    height: 29px;
    border: none;
    outline: none;
    padding: 4px 6px;
}
.content-cell {
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 超出部分隐藏 */
  text-overflow: ellipsis; /* 使用省略号代替隐藏的内容 */
  display: -webkit-box; /* 用于作为弹性盒子布局方式 */
  -webkit-box-orient: vertical; /* 垂直方向排列 */
  -webkit-line-clamp: 1; /* 最多显示两行 */
}
 ul{
    display: flex;
    flex-wrap: wrap;
    margin-top: 10px;

}

ul li {
  box-shadow: 0px 0px 0px rgba(0,0,0,0.2);
  transition: box-shadow 0.3s;
  border:1px solid #EBEEF5;
}

ul li:hover {
  box-shadow: 3px 5px 10px rgba(0,0,0,0.3);
}
 ul li{
    list-style: none;
    width: 205px;
    height: 240px;
    margin-right: 18px;
      /* border: 1px solid #000; */
    background-color: #fff;
     border-radius: 5px;
     margin-bottom: 30px;
}
ul li:nth-child(5n){
    margin-right: 0;
}
li img{
    width: 100%;
    height: 120px;
     border-radius: 5px;

}
li .count{
    margin-top: 15px;
    display: flex;
    color:#999;
    /* margin-bottom: 5px; */
    font-size: 14px;

}
li .datatime {
    color:#999;
    font-size: 14px;
}
li .count p{
    margin-right: 20px;
}
li .miaoshu{
    padding: 0 10px;
}
li .depart{
    font-size: 14px;
    color:#ccc;

}
.dot {
    display: inline-block;
    width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}


.example-pagination-block .example-demonstration {
  margin-bottom: 16px;

}
.example-pagination-block {
  width: 200px;
  height: 200px;
  margin: auto;
}
.el-icon{
    cursor: pointer;
}
.nav-left .active{
    color: red;
}

</style>